﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Filters;
using MensUnoShop.Models;

namespace MensUnoShop.Controllers
{
    public class ApiController : Controller
    {
        //
        // GET: /Api/
        //設定購物車
        [NoCacheFilter]
        public JsonResult stc(Guid Mid, Guid ProductId, string SpecId, int AmountId, Config.MarketType Mktype)
        {         
            
            ShoppingCart sc = ShoppingCart.GetShoppingCart();
            Guid scguid = sc.SCGUID;
            string error = string.Empty;
            string k = string.Empty;
            if (sc.ChkMarket(Mid, ProductId, SpecId, AmountId,Mktype, out error))
            {
                sc.CalShoppingCart();
                sc.SaveToCookie();

                return Json(new { code = "200", response = scguid.ToString("N") });
            }
            else
                return Json(new { code = "500", response = error });
        }

        [NoCacheFilter]
        public JsonResult combostc(Guid[] Mid, Guid[] ProductId, Guid[] SpecId, int comboid)
        {
            string error = string.Empty;
            if (MarketFactory.CheckComboMarketList((long)comboid, ref error))
            {
                ShoppingCart sc = ShoppingCart.GetShoppingCart();
                Guid scguid = sc.SCGUID;
                
                string k = string.Empty;
                if (sc.ChkMarket(Mid, ProductId, SpecId, comboid, out error))
                {
                    sc.CalShoppingCart();
                    sc.SaveToCookie();
                    string pid = "tempcombolist_" + comboid;
                    Session[pid] = null;
                    return Json(new { code = "200", response = scguid.ToString("N") });
                }
                else
                    return Json(new { code = "500", response = error });
            }
            else
                return Json(new { code = "501", response = error });
        }

        [NoCacheFilter]
        public JsonResult jtc(Guid Mid, Guid ProductId, string SpecId, int AmountId, Config.MarketType Mktype)
        {

            ShoppingCart sc = ShoppingCart.GetShoppingCart();
            sc.RemoveAll();
            Guid scguid = sc.SCGUID;
            string error = string.Empty;
            string k = string.Empty;
            if (sc.ChkMarket(Mid, ProductId, SpecId, AmountId, Mktype, out error))
            {
                sc.CalShoppingCart();
                sc.SaveToCookie();

                return Json(new { code = "200", response = scguid.ToString("N") });
            }
            else
                return Json(new { code = "500", response = error });
        }

        [NoCacheFilter]
        public JsonResult getcombo(long id)
        {
            List<TinyMarket> templist = MarketFactory.GetTempComboMarketList(id);
            if (templist.Count > 0)
                return Json(new { code = "200", mklist = templist }, JsonRequestBehavior.AllowGet);
            else
                return Json(new { code = "500", response = "" }, JsonRequestBehavior.AllowGet);
        }

        [NoCacheFilter]
        public JsonResult combosetin(TinyMarket tm)
        {
            MarketFactory.SetTempComboMarketListIn(tm);
            List<TinyMarket> templist = MarketFactory.GetTempComboMarketList(tm.COMBOID);
            if (templist.Count > 0)
                return Json(new { code = "200", mklist = templist });
            else
                return Json(new { code = "500", response = "" });
        }

        [NoCacheFilter]
        public JsonResult combosetout(long tagid, string mkguid, string specid)
        {
            MarketFactory.SetTempComboMarketListOut(tagid, mkguid, specid);
            List<TinyMarket> templist = MarketFactory.GetTempComboMarketList(tagid);
            if (templist.Count > 0)
                return Json(new { code = "200", mklist = templist });
            else
                return Json(new { code = "500", response = "" });
        }

        [NoCacheFilter]
        public string gettamorder(string id)
        {
            string kk = string.Empty;
            Order o = OrderFactory.GetTempOrder(id);
            foreach (var m in o.MLIST)
            {
                kk += "MOKGUID: "+ m.MOKGUID + "<br/>";
                kk += "Oguid: " + o.OGUID + "<br/>";
                kk += "MKGUID: " + m.MKGUID + "<br/>";
                kk += "SPECID: " + m.SPECID + "<br/>";
                kk += "TITLE: " + m.TITLE + "<br/>";
                kk += "SN: " + m.SN + "<br/>";
                kk += "IMG: " + m.IMG + "<br/>";
                kk += "SINGLEPRICE: " + m.SINGLEPRICE + "<br/>";
                kk += "AMOUNT: " + m.AMOUNT + "<br/>";
                kk += "PRICE: " + m.PRICE + "<br/>";
                kk += "COLOR: " + m.COLOR + "<br/>";
                kk += "TYPE: " + m.TYPE + "<br/>";
                kk += "COMBOID: " + m.COMBOID + "<br/>";

                kk += "----------------------------------<br/>";
            }
            return kk;
        }

        [NoCacheFilter]
        public JsonResult setedm(string email)
        {
            if (MemberFactory.SetEdm(email))
                return Json(new { code = "200" });
            else
                return Json(new { code = "500", response = "請稍候再試!!" });
        }
        }
}
